package com.bci.internal.engine.mybatisplus;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.bci.internal.engine.bcibpmengine.utils.BCStringUtils;


import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 基于MyBatis-plus的代码生成器
 *
 * @author L.X
 */
public class JavaCodeGenerator {

    /**
     * 读取控制台内容
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
        System.out.println("请输入" + tip + ": ");
        if (scanner.hasNext()) {
            String next = scanner.next();
            if (BCStringUtils.hasText(next)) {
                return next;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {
        /* 代码生成器 */
        AutoGenerator mpg = new AutoGenerator();

        /* 全局配置 */
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        gc.setAuthor("jzwu");
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setOpen(false);
        // gc.setEntityName("%sModel");
        // gc.setMapperName("%DAO");
        gc.setServiceName("%sService");
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        /* 数据源配置 */
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/local_kpi?useSSL=true&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root123456");
        mpg.setDataSource(dsc);

        /* 包配置 */
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.bci.internal.engine.bcibpmengine");
        mpg.setPackageInfo(pc);

        /* 配置模板 */
        TemplateConfig templateConfig = new TemplateConfig();
        /* 指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 */
        templateConfig.setEntity("templates/entity.java");
        templateConfig.setController("templates/controller.java");
        templateConfig.setMapper("templates/mapper.java");
        templateConfig.setService("templates/service.java");
        // templateConfig.setXml("/templates/mapper.xml");
        mpg.setTemplate(templateConfig);

        /* 策略配置 */
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude(scanner("表名, 多个英文逗号分割").split(","));
        // strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("");
        mpg.setStrategy(strategy);
        // mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // mpg.setTemplateEngine(new BeetlTemplateEngine());
        mpg.execute();
    }

}
